﻿Imports System.Data
Imports System.Data.OleDb
Public Class frmPhanQuyen
    Private connect As ConnectDB
    Private lenh As String

    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

    Private Sub PhanQuyen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadQuyen()
    End Sub

    Private Sub LoadQuyen()
        Dim lenh As String
        If frmDangNhap.LoginLoaiND = "Quan_Ly" OrElse frmDangNhap.LoginLoaiND = "Admin" Then
            grb_PhanQuyen.Enabled = True
            lenh = "Select * from PHANQUYEN where IDLoaiNV ='" + frmMain.cmbUserName.Text + "'"
        Else
            grb_PhanQuyen.Enabled = False
            btn_DongY.Enabled = False
            lenh = "Select * from PHANQUYEN where IDLoaiNV ='" + frmDangNhap.LoginTenND.ToString + "'"
        End If
        txt_IdNhanVien.Text = frmMain.cmbUserName.Text
        connect = New ConnectDB()
        Dim i As Integer
        Dim dt As New DataTable
        dt = connect.GetData(lenh)
        For i = 0 To dt.Columns.Count() - 1
            dt.Rows(0)(i).ToString()
        Next
        connect.Close()

        If dt.Rows(0)(1).ToString = 1 Then
            ckbDT.Checked = True
        End If
        If dt.Rows(0)(5).ToString = 1 Then
            ckbNT.Checked = True
        End If
        If dt.Rows(0)(4).ToString = 1 Then
            ckbNN.Checked = True
        End If
        If dt.Rows(0)(6).ToString = 1 Then
            ckbKV.Checked = True
        End If
        If dt.Rows(0)(3).ToString = 1 Then
            ckbLN.Checked = True
        End If
        If dt.Rows(0)(2).ToString = 1 Then
            ckbTT.Checked = True
        End If

    End Sub

    Private Sub btn_DongY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DongY.Click
        Dim dt, tn, nt, kv, ln, tt As String
        dt = CStr(0)
        tn = CStr(0)
        nt = CStr(0)
        kv = CStr(0)
        ln = CStr(0)
        tt = CStr(0)
        If ckbDT.Checked = True Then
            dt = CStr(1)
        End If
        If ckbNT.Checked = True Then
            tn = CStr(1)
        End If
        If ckbNN.Checked = True Then
            nt = CStr(1)
        End If
        If ckbKV.Checked = True Then
            kv = CStr(1)
        End If
        If ckbLN.Checked = True Then
            ln = CStr(1)
        End If
        If ckbTT.Checked = True Then
            tt = CStr(1)
        End If
        Dim dlg As DialogResult = MessageBox.Show("Bạn có chắc chắn muốn cấp quyền cho nhân viên " & txt_IdNhanVien.Text & "!", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dlg = Windows.Forms.DialogResult.Yes Then
            'Duyet trong bang phan quyen xem co nhan vien ten do chua, neu chua thi insert vao
            connect = New ConnectDB()
            Dim flag As Integer = 0
            Dim i As Integer
            Dim bang As New DataTable
            bang = connect.GetData("Select IDLoaiNV from PHANQUYEN")
            For i = 0 To bang.Rows.Count() - 1
                bang.Rows(i)("IDLoaiNV").ToString()
            Next
            connect.Close()
            For i = 0 To bang.Rows.Count() - 1
                If bang.Rows(i)("IDLoaiNV").ToString = txt_IdNhanVien.Text Then
                    flag = 1
                End If
            Next
            If flag = 0 Then
                lenh = "Insert into PHANQUYEN values('" + txt_IdNhanVien.Text + "', " + dt + ", " + tt + ", " + ln + ", " + nt + ", " + tn + ", " + kv + ")"
            Else
                lenh = "Update PHANQUYEN set MaDangTin = '" + dt + "', MaThongTinNha = '" + tt + "', MaLoai = '" + ln + "', MaNguoiNhan = '" + nt + "', MaNguoiThue = '" + tn + "', MaKhuVuc = '" + kv + "' where IDLoaiNV = '" + txt_IdNhanVien.Text + "'"
            End If
            'MessageBox.Show(lenh)
            Try
                connect = New ConnectDB()
                connect.Execute(lenh)
                connect.Close()
                MessageBox.Show("Nhân viên " & txt_IdNhanVien.Text & " đã được cấp quyền!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show("Cấp quyền không thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            End Try
        Else
            MessageBox.Show("Đã hủy thao tác cấp quyền, bạn có thể câp quyền lại hoặc thoát ra!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        connect.Close()
        'UpdateQuyen()
    End Sub

    Public Sub UpdateQuyen()
        connect = New ConnectDB()
        Dim i As Integer
        Dim dt As New DataTable
        dt = connect.GetData("select * from PHANQUYEN where IDLoaiNV ='" + frmDangNhap.LoginTenND + "'")
        For i = 0 To dt.Columns.Count() - 1
            dt.Rows(0)(i).ToString()
        Next
        connect.Close()

        If CInt(dt.Rows(0)(1).ToString) = 0 Then
            frmMain.TabItem_DangTin.Visible = False
            frmMain.btnDangTin.Enabled = False
        End If
        If CInt(dt.Rows(0)(5).ToString) = 0 Then
            frmMain.TabItem_ThueNha.Visible = False
            frmMain.btnThueNha.Enabled = False
        End If
        If CInt(dt.Rows(0)(4).ToString) = 0 Then
            frmMain.TabItem_NhanTin.Visible = False
            frmMain.btnNhanTin.Enabled = False
        End If
        If CInt(dt.Rows(0)(3).ToString) = 0 Then
            frmMain.TabItem_LN_KV.Visible = False
            frmMain.btnLN_KV.Enabled = False
        End If
        If CInt(dt.Rows(0)(2).ToString) = 0 Then
            frmMain.TabItem_TTN.Visible = False
            frmMain.btnQL_TTN.Enabled = False
        End If
        If CInt(dt.Rows(0)(6).ToString) = 0 Then
            frmMain.TabItem_LN_KV.Visible = False
            frmMain.btnLN_KV.Enabled = False
        End If
    End Sub

End Class